//
//  XHComposeTabar.swift
//  gu-新浪微博
//
//  Created by xiaohui on 16/7/3.
//  Copyright © 2016年 gusheghui. All rights reserved.
//

import UIKit

enum XHComposeTabarType:Int{
    
    case pictureButton = 10
    case mentionButton = 20
    case trendButton = 30
    case emoticonButton = 40
    case addButton = 50
}

class XHComposeTabar: UIView {

    //定义一个闭包
        var closure:((type: XHComposeTabarType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUi()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUi(){
        
        backgroundColor = UIColor.whiteColor()
        
        let pictureButton = addChildButton("compose_toolbar_picture",type:.pictureButton)
        let mentionButton = addChildButton("compose_mentionbutton_background",type: .mentionButton)
        let trendButton = addChildButton("compose_trendbutton_background",type: .trendButton)
        let emoticonButton = addChildButton("compose_emoticonbutton_background",type: .emoticonButton)
        let addButton = addChildButton("compose_add_background",type: .addButton)

        pictureButton.snp_makeConstraints { (make) -> Void in
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        mentionButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureButton.snp_trailing)
            make.width.equalTo(trendButton)
        }
        trendButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionButton.snp_trailing)
            make.width.equalTo(emoticonButton)
        }
        emoticonButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendButton.snp_trailing)
            make.width.equalTo(addButton)
        }

        addButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emoticonButton.snp_trailing)
        }
    }
    
    func buttonClick(button: UIButton){
        closure?(type: XHComposeTabarType(rawValue: button.tag)!)
    }
    
    private func addChildButton(imageName:String,type:XHComposeTabarType)->UIButton{
        
        let button = UIButton()
        button.tag = type.rawValue
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        addSubview(button)
        return button
    }
        
}
